import uuid
from dataclasses import dataclass
from typing import Any

import pytest
from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumeSkillGateway
from recruit.adapter.database.models import Resume, ResumeSkill
from .fixtures.resume import ResumeTest
from .utils import check_attr_model, delete_user, check_get_one, get_resume


@dataclass
class ResumeSkillTest:
    id: uuid.uuid4()
    resume_id: uuid.uuid4()
    skill: str


async def _create_resume_skill(
        session: AsyncSession,
        data: dict[str, Any]
) -> ResumeSkillTest:
    stmt = insert(ResumeSkill).values(**data).returning(ResumeSkill)
    resume_skill = (await session.execute(stmt)).scalar_one()
    resume_skill = ResumeSkillTest(
        id=resume_skill.id,
        resume_id=resume_skill.resume_id,
        skill=resume_skill.skill
    )
    await session.commit()
    return resume_skill


async def _check_resume(
        session: AsyncSession,
        data: ResumeSkillTest
) -> None:
    resume_skill = await get_resume(session, ResumeSkill, data.id)
    assert isinstance(resume_skill, ResumeSkill)
    check_get_one(
        resume_skill,
        data,
        ('id', 'resume_id', 'skill')
    )


async def test_create(
        db_session: AsyncSession,
        get_resume_skill: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_skill_gateway = ResumeSkillGateway(db_session)
    get_resume_skill['resume_id'] = create_resume.id
    resume_skill = await resume_skill_gateway.create(**get_resume_skill)
    assert isinstance(resume_skill.resume, Resume)
    check_attr_model(resume_skill, get_resume_skill)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'keys', [
        ('id', 'resume_id', 'skill')
    ]
)
async def test_get_one(
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_skill: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_skill_gateway = ResumeSkillGateway(db_session)
    get_resume_skill['resume_id'] = create_resume.id
    created_resume_skill = await _create_resume_skill(
        db_session,
        get_resume_skill
    )
    for key, value in get_resume_skill.items():
        resume_skill = await resume_skill_gateway.get_one(**{key: value})
        assert resume_skill is not None
        check_get_one(resume_skill, created_resume_skill, keys)
    await delete_user(db_session, create_resume.user_id)


async def test_check_resume(
        db_session: AsyncSession,
        get_resume_skill: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_skill_gateway = ResumeSkillGateway(db_session)
    get_resume_skill['resume_id'] = create_resume.id
    created_resume_skill = await _create_resume_skill(
        db_session,
        get_resume_skill
    )
    val = await resume_skill_gateway.check_resume(
        created_resume_skill.resume_id
    )
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_skill_gateway.check_resume(
        created_resume_skill.resume_id
    )
    assert not val


async def test_delete(
        db_session: AsyncSession,
        get_resume_skill: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_skill_gateway = ResumeSkillGateway(db_session)
    get_resume_skill['resume_id'] = create_resume.id
    created_resume_skill = await _create_resume_skill(
        db_session,
        get_resume_skill
    )
    await resume_skill_gateway.delete(created_resume_skill.id)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)
    resume_skill = await get_resume(
        db_session,
        ResumeSkill,
        created_resume_skill.id
    )
    assert resume_skill is None
